package com.example.workflow.listener;

import com.example.workflow.mapper.UserInfoMapper;
import com.example.workflow.pojo.UserInfo;
import com.example.workflow.service.UserInfoService;
import com.example.workflow.util.MailUtils;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
public class CompleteListener implements TaskListener {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private MailUtils mailUtils;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void notify(DelegateTask delegateTask) {
        String taskId = delegateTask.getTaskDefinitionKey();
        String assignee = delegateTask.getAssignee();
        String processInstanceId = delegateTask.getProcessInstanceId();
        String businessInstanceId = delegateTask.getExecution().getProcessBusinessKey();
//        log.info("任务完成"+businessInstanceId);
        log.info("任务[ {} ]已经完成，处理人员为: {}",taskId,assignee);
        // 通知creator
        UserInfo userInfo = userInfoService.getUserInfoByOrder(Long.getLong(businessInstanceId));
//        log.info(userInfo);

//        TODO 通知有关人员
//        mailUtils.sendMail(userInfo.getEmail(),null,null,"工单完成通知","尊敬的用户，您好：\n您申请的工单流程已完成，请登录系统查阅！");

    }
}
